package com.atguigu.gmall.realtime.utils;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.common.GmallConfig;
import io.debezium.data.Json;
import redis.clients.jedis.Jedis;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @author jason
 * @create 2022-04-16 16:33
 */
public class DimUtil {
    public static JSONObject getDimInfo(Connection connection, String tableName, String id) throws SQLException,
            InvocationTargetException, InstantiationException, IllegalAccessException {
//        拼接查询语句
//        select * from db.tn where id ='18';
        String querySql = "select * from " + GmallConfig.HBASE_SCHEMA
                + "." + tableName
                + " where id='" + id + "'";
//        查询phoenix之前先去查询Redis
        Jedis jedis = RedisUtil.getJedis();
//        jedis.expire()  不选Hash 1.用户维度数据量大 2.设置过期时间  生产多条机器Hash热点问题  不选 set 查询不方便
//        DIM:DIM_USER_INF:143
        String redisKey = "DIM:" + tableName + ":" + id;
        String dimInfoJsonStr = jedis.get(redisKey);
        if (dimInfoJsonStr != null) {

//           重置过期时间
            jedis.expire(redisKey, 24 * 60 * 60);
            //            归还连接
            jedis.close();
            //            返回结果
            return JSONObject.parseObject(dimInfoJsonStr);
        }

//        查询phoenix
        List<JSONObject> queryList = JdbcUtil.queryList(connection, querySql
                , JSONObject.class
                , false);
        JSONObject dimInfoJson = queryList.get(0);
//        返回结果之前把数据写入Redis
        jedis.set(redisKey, dimInfoJson.toJSONString());
        jedis.expire(redisKey, 24 * 60 * 60);
        jedis.close();

//        返回结果
        return queryList.get(0);
    }

    public static void delRedisDimInfo(String tableName, String id) {
        Jedis jedis = RedisUtil.getJedis();
        String redisKey = "DIM:" + tableName + ":" + id;
        jedis.del(redisKey);
        jedis.close();
    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException, InvocationTargetException,
            InstantiationException, IllegalAccessException {
        Class.forName(GmallConfig.PHOENIX_DRIVER);
        Connection connection = DriverManager.getConnection(GmallConfig.PHOENIX_SERVER);

        long start = System.currentTimeMillis();
        System.out.println(getDimInfo(connection, "DIM_BASE_TRADEMARK", "12"));

//        long end = System.currentTimeMillis();
////延迟时间
//        System.out.println(getDimInfo(connection, "DIM_USER_INFO", "1433"));
//        long end2 = System.currentTimeMillis();
//
//        System.out.println(getDimInfo(connection, "DIM_USER_INFO", "1433"));
//        long end3 = System.currentTimeMillis();
//
//        System.out.println(end - start);
//        System.out.println(end2 - end);
////        ./redis-cli -h hadoop101 --raw  可以看中文
//        System.out.println(end3 - end2);

        connection.close();
    }
}
